
var RLines = require('./renderLines');
class DxfStruct {
    constructor(dxf) {
        this.dxf = dxf;
    }
    getDxfStruct() {
        var res = { blocks: [], layers: [] }
        for (var key in this.dxf.blocks) {
            res.blocks.push(key)
        }
        for (var key in this.dxf.tables.layer.layers) {
            res.layers.push(key)
        }
        return res
    }
    /*
    layers:显示的图层
    blocks:显示的块
    merge:合并的图层
    */
    setShowLayers(layers, blocks, merge, nlayer) {
        if (layers) {
            this.layers = layers
        }

        if (blocks) {
            this.blocks = blocks
        }

        if (merge) {
            this.merge = merge
        }

        if (nlayer) {
            this.nlayer = nlayer
        }
    }
    checkBlock(name) {
        if (this.blocks) {
            if (this.blocks.indexOf(name) >= 0) {
                return true
            }
            else {
                return false
            }
        }
        return true
    }
    checkLayer(name) {
        if (this.layers) {
            if (this.layers.indexOf(name) >= 0) {
                return true
            }
            else {
                return false
            }
        }
        return true
    }
    getBlockEntity(en, data, p) {
        var res = [], po = { x: 0, y: 0, z: 0 }, self = this
        switch (en.type) {
            case 'DIMENSION':
                if (!en.view && en.position) {
                    en.view = en.position
                }
                console.log(en.block)
                // for(var i = 0;i < data.blocks[en.block].entities.length;i++){
                //     data.blocks[en.block].entities[i].view = en.view
                //     res = res.concat(getBlockEntity(data.blocks[en.block].entities[i],data))
                // }
                break;
            case 'INSERT':
                if (self.checkBlock(en.name)) {
                    if (en.position) {
                        po.x = en.position.x + p.x
                        po.y = en.position.y + p.y
                        po.z = en.position.z + p.z
                    }
                    else {
                        po = p
                    }
                  
                    for (var i = 0; i < data.blocks[en.name].entities.length; i++) {
                        var temp = data.blocks[en.name].entities[i]
                        if (typeof temp.flag != "undefined") {
                            temp.flag += 1
                        }
                        else {
                            temp.flag = 1
                        }
                        //if(temp.flag > 1)
                        //console.log(en.name)
                        res = res.concat(self.getBlockEntity(temp, data, po))
                    }
                }
                break;
            default:
                if (typeof en.flag == "undefined" || (typeof en.flag != "undefined" && en.flag == 1)) {
                    if (self.checkLayer(en.layer)) {
                        if (en.vertices) {
                            en.vertices = en.vertices.map(function (e) {
                                e.x += p.x
                                e.y += p.y
                                e.z += p.z
                                return e
                            })
                        }
                        else if (en.startPoint) {
                            en.startPoint.x += p.x
                            en.startPoint.y += p.y
                            en.startPoint.z += p.z
                        }
                        else if (en.position && en.type == "POINT") {
                            en.position.x += p.x
                            en.position.y += p.y
                            en.position.z += p.z
                        }
                        else if (en.center ) {
                            en.center.x += p.x
                            en.center.y += p.y
                            en.center.z += p.z
                        }
                        //res_struct[en.type].push(en)
                        switch (en.type) {
                            case 'TEXT':
                            case 'MTEXT':
                                //en.layer = '标注标识'
                                res.push(en)
                                break;
                            case 'LWPOLYLINE':
                            case 'LINE':
                            case 'POLYLINE':
                            //     //en.layer = '建筑及道路'
                            //     if(en.layer == '给水'){
                            //         res_struct.push(en)
                            //     }
                            //     else{
                            //         if (en.lineType) {
                            //             var lineType = data.tables.lineType.lineTypes[en.lineType];
                            //             if (lineType && lineType.pattern && lineType.pattern.length !== 0) {
                            //                 en.lineType = 'sold'
                            //             }
                            //         }
                            //         res.push(en)
                            //     }
                            // break;
                            default:
                                //en.layer = '建筑及道路'
                                res.push(en)
                                break;
                        }
                        //res.push(en)
                    }
                }
                break;
        }
        return res
    }
    /*输出格式类型
    info:输出图层信息和数量
    merge:图层内部合并
    user:图层合并和线段定义，用于平台显示
    */
    resetDxf(type) {
        var self = this, res = [], point = { x: 0, y: 0, z: 0 }

        self.dxf.entities.map(function (e) {
            res = res.concat(self.getBlockEntity(e, self.dxf, point))
        })

        self.dxf.entities = res
        switch (type) {
            case 'info':
                self.toResoult()
                break;
            case 'merge':
                self.mergeLayer()
                self.toResoult()
                break;
            case 'user':
                self.mergeLayer()
                self.renderLayer()
                self.toResoult()
                break;
        }
        
    }
    renderLayer() {
        var self = this, res = [], nlayer = []

        self.dxf.blocks = []

        self.dxf.entities.map(function (e) {
            if (typeof self.nlayer[e.layer] != "undefined") {
                //console.log(e.type)
                switch (e.type) {
                    case 'LWPOLYLINE':
                    case 'LINE':
                    case 'POLYLINE':
                        if (typeof e.color != "undefined" && self.nlayer[e.layer][e.color]) {
                            e.layer = self.nlayer[e.layer][e.color]
                            nlayer.push(e)
                        }
                        break;
                }
                //dxf.tables.layer.layers = tlayers
            }
            else {
                res.push(e)
            }
        })
        self.dxf.entities = res
        //重新设置layer
        console.log('nlayer',res.length,nlayer.length)
        
        var tlayers = {}
        for (var key in self.nlayer) {
            for(var i in self.nlayer[key]){
                tlayers[self.nlayer[key][i]] = { color: i, name: self.nlayer[key][i], b: true }
            }
        }
        for (var key in self.dxf.tables.layer.layers) {
            if (self.layers.indexOf(key) >= 0 && !self.nlayer[key]) {
                tlayers[key] = { color: self.dxf.tables.layer.layers[key].color, name: key }
            }
        }
        self.dxf.tables.layer.layers = tlayers

        //管线定义编号
        nlayer = nlayer.map(function (e, i) {
            e.userData = { id: i, chr: [], layer: e.layer,p: [0,0] }
            return e
        })
        console.log('nlayer',nlayer.length)

        var rlines = new RLines(0.1, nlayer)
        rlines.action()
        
        self.dxf.sys.lines = rlines.lines.map(function (e, i) {
            var struct = { flag: 'fat' }, chr = [], len = 0
            struct.type = e.type
            struct.layer = e.layer
            struct.userData = e.userData

            for (var index = 0; index < e.vertices.length - 1; index++) {
                len += Math.sqrt(Math.pow(e.vertices[index].x - e.vertices[index + 1].x, 2) + Math.pow(e.vertices[index].y - e.vertices[index + 1].y, 2))
            }
            struct.userData.len = len

            rlines.lines.map(function (l) {
                if (e.userData.chr.indexOf(l.userData.id) >= 0) {
                    chr.push({ dn: parseInt(l.layer.replace('DN', '')), id: l.userData.id })
                }
            })
            struct.userData.chr = chr

            struct.vertices = []
            e.vertices.map(function (p) {
                struct.vertices.push(p.x, p.y, 0)
            })

            if (e.userData && e.userData.p) {
                if (e.userData.p[0] >= 2) {
                    self.dxf.sys.points.push(e.vertices[0])
                }
                if (e.userData.p[1] >= 2) {
                    self.dxf.sys.points.push(e.vertices[e.vertices.length - 1])
                }
            }
            return struct
        })

        console.log('renderLayer',self.dxf.entities.length,self.dxf.sys.lines.length)
    }
    mergeLayer() {
        var self = this, merge = {}, res = []
        self.merge.map(function (e) {
            if (!res[e]) {
                merge[e] = {
                    points: [],
                    indices: [],
                    //colors:[],
                    type: 'LINE',
                    flag: 'block',
                    layer: e
                }
            }
        })
        //console.log('res_build',res_build.length)
        self.dxf.entities.map(function (e) {
            if (self.merge.indexOf(e.layer) >= 0) {
                switch (e.type) {
                    case 'LWPOLYLINE':
                    case 'LINE':
                    case 'POLYLINE':
                        if (e.shape) {
                            e.vertices = e.vertices.concat([e.vertices[0]])
                        }
                        e.vertices.map(function (p, i) {
                            if (i == 0 || i == e.vertices.length - 1) {
                                merge[e.layer].indices.push(merge[e.layer].points.length / 3)
                            }
                            else {
                                merge[e.layer].indices.push(merge[e.layer].points.length / 3, merge[e.layer].points.length / 3)
                            }
                            merge[e.layer].points.push(p.x, p.y, 0)
                        })
                        break;
                        defalut:
                        res.push(e)
                        break;
                }
            }
            else {
                res.push(e)
            }
        })
        for (var key in merge) {
            res.push(merge[key])
            console.log(key)
        }
        self.dxf.entities = res
        console.log('merge',self.dxf.entities.length)
    }
    toResoult() {
        var layer_color = {}

        this.dxf.entities.map(function (e) {
            if (e.color) {
                if (!layer_color[e.layer]) {
                    layer_color[e.layer] = {}
                }

                if (!layer_color[e.layer][e.color]) {
                    layer_color[e.layer][e.color] = 0
                }
                layer_color[e.layer][e.color]++
            }
            if (e.type) {
                if (!layer_color[e.layer]) {
                    layer_color[e.layer] = {}
                }

                if (!layer_color[e.layer][e.type]) {
                    layer_color[e.layer][e.type] = 0
                }
                layer_color[e.layer][e.type]++
            }
        })

        console.log(layer_color)
    }
    action() {
        //修改静态变量
        this.renderEntity(this.lines)
        //console.log(this.lines)
    }
}
module.exports = DxfStruct;